package com.danciyixue.boot.client.dailyTimeRecords.contoller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.danciyixue.boot.common.result.PageResult;
import com.danciyixue.boot.common.result.Result;
import com.danciyixue.boot.dcyx.model.form.DailyTimeRecordsForm;
import com.danciyixue.boot.dcyx.model.query.DailyTimeRecordsQuery;
import com.danciyixue.boot.dcyx.model.vo.DailyTimeRecordsVO;
import com.danciyixue.boot.dcyx.service.DailyTimeRecordsService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * 用户每日时间记录前端控制层
 *
 * @author youlaitech
 * @since 2025-06-15 16:22
 */
@Tag(name = "06. 在线时间", description = "在线时间")
@RestController
@RequestMapping("/api/client/v1/daily-time-records")
@RequiredArgsConstructor
public class DcyxDailyTimeRecordsController {

    private final DailyTimeRecordsService dailyTimeRecordsService;

    /**
     * 获取今日在线时长
     */
    @GetMapping("/today-online-seconds")
    @Operation(summary = "获取今日在线时长")
    public Result<DailyTimeRecordsVO> getTodayOnlineSeconds() {
        DailyTimeRecordsVO vo = dailyTimeRecordsService.getOrCreateTodayOnlineSeconds();
        return Result.success(vo);
    }

    /**
     * 更新今日在线时长
     */
    @PostMapping("/add-online-seconds")
    @Operation(summary = "增加今日在线时长")
    public Result<Void> addTodayOnlineSecond(@RequestBody DailyTimeRecordsForm dailyTimeRecordsForm) {
        boolean result = dailyTimeRecordsService.addTodayOnlineSeconds(dailyTimeRecordsForm);
        return Result.judge(result);
    }


    @Operation(summary = "用户每日时间记录分页列表")
    @GetMapping("/page")
    public PageResult<DailyTimeRecordsVO> getDailyTimeRecordsPage(DailyTimeRecordsQuery queryParams ) {
        IPage<DailyTimeRecordsVO> result = dailyTimeRecordsService.getDailyTimeRecordsPage(queryParams);
        return PageResult.success(result);
    }

    @Operation(summary = "新增用户每日时间记录")
    @PostMapping
    public Result<Void> saveDailyTimeRecords(@RequestBody @Valid DailyTimeRecordsForm formData ) {
        boolean result = dailyTimeRecordsService.saveDailyTimeRecords(formData);
        return Result.judge(result);
    }

    @Operation(summary = "获取用户每日时间记录表单数据")
    @GetMapping("/{id}/form")
    public Result<DailyTimeRecordsForm> getDailyTimeRecordsForm(
        @Parameter(description = "用户每日时间记录ID") @PathVariable Long id
    ) {
        DailyTimeRecordsForm formData = dailyTimeRecordsService.getDailyTimeRecordsFormData(id);
        return Result.success(formData);
    }

    @Operation(summary = "修改用户每日时间记录")
    @PutMapping(value = "/{id}")
    public Result<Void> updateDailyTimeRecords(
            @Parameter(description = "用户每日时间记录ID") @PathVariable Long id,
            @RequestBody @Validated DailyTimeRecordsForm formData
    ) {
        boolean result = dailyTimeRecordsService.updateDailyTimeRecords(id, formData);
        return Result.judge(result);
    }

    @Operation(summary = "删除用户每日时间记录")
    @DeleteMapping("/{ids}")
    public Result<Void> deleteDailyTimeRecordss(
        @Parameter(description = "用户每日时间记录ID，多个以英文逗号(,)分割") @PathVariable String ids
    ) {
        boolean result = dailyTimeRecordsService.deleteDailyTimeRecordss(ids);
        return Result.judge(result);
    }
}
